package com.jshop.utils;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.jshop.model.Brand;
import com.jshop.model.Category;
import com.jshop.model.Product;
import com.jshop.model.Rule;

public class DatagridUtil {
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Map getList(Page page){
		Map map = new HashMap();
		map.put("rows", page.getList());  
		map.put("total", page.getTotalRow()); 
		return map;
	}
	public static String getProductList(Page page) throws JSONException{
		JSONObject obj=new JSONObject();
		JSONArray list=new JSONArray();
		for(int i=0;i<page.getList().size();i++){
			Product p=(Product) page.getList().get(i);
			JSONObject pobj=new JSONObject(JsonKit.toJson(p));
			Category c=Category.dao.findById(p.getCategoryId());
			if(c!=null){
				pobj.put("category_name", c.getCategoryName());
			}else{
				pobj.put("category_name", "");
			}
			Brand b=Brand.dao.findById(p.getRuleId());
			if(b!=null){
				pobj.put("brand_name", b.getBrandName());
			}else{
				pobj.put("brand_name", "");
			}
			Rule r=Rule.dao.findById(p.getRuleId());
			if(r!=null){
				pobj.put("rule_name", r.getRuleName());
			}else{
				pobj.put("rule_name", "");
			}
			list.put(pobj);
		}
		obj.put("rows", list);
		obj.put("total", page.getTotalRow());
		return obj.toString();
	}
}
